package entity;

import constant.Constant;
import enumeration.Action;
import enumeration.R;

public class Environment {

	private static int[] env = new int[Constant.WIDTH * Constant.HEIGHT];
	private static int winIndex;
	private static int[] loseIndex;

	public void init() {
		winIndex = (Constant.WIDTH - 1) * Constant.HEIGHT - 2;
		loseIndex = new int[Constant.LOSE_COUNT];
		if (loseIndex.length > 0) {
			loseIndex[0] = winIndex - 1;
		}
		if (loseIndex.length > 1) {
			loseIndex[1] = winIndex - Constant.WIDTH;
		}
		if (loseIndex.length == 3) {
			loseIndex[2] = winIndex + 1;
		}
		env[winIndex] = Constant.WIN_POINT;
		for (int i = 0; i < loseIndex.length; i++) {
			env[loseIndex[i]] = Constant.LOSE_POINT;
		}
		env[0] = Constant.PLAYER_POINT;
	}

	public NextState updateEnv(Step step, Action action) {
		NextState nextState = new NextState();
		nextState.setR(R.NORMAL);
		switch (action) {
		case LEFT:
			nextState.setNextStep(step.getStep() - 1);
			// 不可行动状态
			if (step.getStep() == 0 || step.getStep() % Constant.WIDTH == 0) {
				nextState.setNextStep(step.getStep());
			}
			// 胜利状态
			else if (step.getStep() == winIndex + 1) {
				nextState.setR(R.WIN);
			}
			// 失败状态
			else if (loseIndex.length > 1 && step.getStep() == loseIndex[1] + 1) {
				nextState.setR(R.LOSE);
			}
			// 普通状态
			break;
		case RIGHT:
			nextState.setNextStep(step.getStep() + 1);
			// 不可行动状态
			if (step.getStep() != 0 && (step.getStep() + 1 ) % Constant.WIDTH == 0) {
				nextState.setNextStep(step.getStep());
			}
			// 胜利状态
			// 失败状态
			else if ((loseIndex.length > 0 && step.getStep() == loseIndex[0] - 1)
					|| (loseIndex.length > 1 && step.getStep() == loseIndex[1] - 1)) {
				nextState.setR(R.LOSE);
			}
			// 普通状态
			break;
		case UP:
			nextState.setNextStep(step.getStep() - Constant.WIDTH);
			// 不可行动状态
			if (step.getStep() < Constant.WIDTH) {
				nextState.setNextStep(step.getStep());
			}
			// 胜利状态
			else if (step.getStep() == winIndex + Constant.WIDTH) {
				nextState.setR(R.WIN);
			}
			// 失败状态
			else if ((loseIndex.length > 0 && step.getStep() == loseIndex[0] + Constant.WIDTH)
					|| (loseIndex.length == 3 && step.getStep() == loseIndex[2] + Constant.WIDTH)) {
				nextState.setR(R.LOSE);
			}
			// 普通状态
			break;
		case DOWN:
			nextState.setNextStep(step.getStep() + Constant.WIDTH);
			// 不可行动状态
			if (step.getStep() >= (Constant.WIDTH * (Constant.HEIGHT - 1))) {
				nextState.setNextStep(step.getStep());
			}
			// 胜利状态
			if (loseIndex.length < 2 && step.getStep() == loseIndex[1] - Constant.WIDTH) {
				nextState.setR(R.WIN);
			}
			// 失败状态
			if ((loseIndex.length > 0 && step.getStep() == loseIndex[0] - Constant.WIDTH)
					|| (loseIndex.length > 1 && step.getStep() == loseIndex[1] - Constant.WIDTH)
					|| (loseIndex.length > 2 && step.getStep() == loseIndex[2] - Constant.WIDTH)) {
				nextState.setR(R.LOSE);
			}
			// 普通状态
			break;
		}
		env[step.getStep()] = Constant.NORMAL_POINT;
		env[nextState.getNextStep()] = Constant.PLAYER_POINT;
		return nextState;
	}

	public static int[] getEnv() {
		return env;
	}

	public static void setEnv(int[] env) {
		Environment.env = env;
	}

}
